# coding=utf-8
from math import *
from ..ToolsConfig import *


def ComBin(i, place, result):
    # type:(int,int,int) -> bool
    """
    提取参数i位于place的结果并于result比较
    例如输入：0b00101,0b10000,0b01110,则会返回false
    """
    return i & place == result & place


def BinCal(i, place, result):
    # type:(int,int,bool) -> int
    """
    为参数i设置位于place的result结果
    例如输入：0b00100,0b00101,True
    输出将会是：0b00101
    """
    return ((i | place) ^ place) | place * result


def CosOfTwoVec(vec1, vec2):
    """
    获取两个相同维度向量的夹角cos值
    """
    if vec1.__len__() != vec2.__len__():
        return 0
    nv1 = Normalize(vec1)
    nv2 = Normalize(vec2)
    _ = 0
    for index in range(nv1.__len__()):
        _ += nv1[index] * nv2[index]
    return _


def Normalize(vec):
    # type:(Union[List,Tuple]) -> Union[List,Tuple]
    """
    将任意维度向量转换为单位向量
    """
    r = 0
    for _ in vec:
        r += _ ** 2.0
    r = r ** 0.5
    cls = vec.__class__
    return cls([(_ / r) for _ in vec])


def GetTwoPointVec(pos1, pos2):
    # type:(Union[List,Tuple],Union[List,Tuple]) -> Tuple
    """
    计算任意两点的方向向量, 非单位向量, pos1 -> pos2
    """
    li = []
    if pos1.__len__() != pos2.__len__():
        return tuple(li)
    for index in range(pos1.__len__()):
        li.append(pos2[index] - pos1[index])
    return tuple(li)


def MatProduct(vec, mat):
    """
    计算向量或者坐标与矩阵相乘, 需要i=j
    """
    new = [0.0 for _ in range(vec.__len__())]
    lengthIndex = range(vec.__len__())
    for _ in lengthIndex:
        for __ in lengthIndex:
            new[_] += vec[__] * mat[_][__]
    return new
